package com.javatom.notes.feign.web;

import com.javatom.notes.common.User;
import com.javatom.notes.common.model.BaseResponse;
import com.javatom.notes.feign.service.UserFeignHystrixFallBackOperatService;
import com.javatom.notes.feign.service.UserFeignHystrixOperatService;
import com.javatom.notes.feign.service.UserFeignOperatService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * feign和hystrix一些使用
 */
@RestController
@RequestMapping(value = "/api/hystrix")
@Slf4j
public class FeignHystrixOperatContrllor {
    @Resource
    private UserFeignHystrixOperatService userFeignHystrixOperatService;
    //弟二种 通过实现fallbackclass 实现

    @Resource
    private UserFeignHystrixFallBackOperatService userFeignHystrixFallBackOperatService;
    @RequestMapping(method = RequestMethod.GET, value = "/list2")
    public BaseResponse<List<User>> getList2(){
        return userFeignHystrixFallBackOperatService.getList();
    }


    //弟一种 直接在contrllor 实现fallbackMethod
    @RequestMapping(method = RequestMethod.GET, value = "/list")
    @HystrixCommand(fallbackMethod = "fillGetList")
    public BaseResponse<List<User>> getList(){
        return userFeignHystrixOperatService.getList();
    }

    /**
     * 错误方法 熔断
     * @return
     */
    private   BaseResponse<List<User>> fillGetList(){
        log.error("错误了 降级处理1");
        return BaseResponse.success(null);
    }

}
